import {login, logout, getInfo} from '@/api/user'
import {getToken, setToken, removeToken} from '@/utils/auth'
import {resetRouter} from '@/router'
import {constantRoutes} from '@/router';
import routerFormat from '@/utils/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    admin_username: '',
    admin_fullname: '',
    routers: constantRoutes,
    addRouters: [],
    id: '',
    schoolId: '',
    group_title: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
    localStorage.clear()
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    localStorage.setItem('token',token);
  },
  SET_schoolId: (state, schoolId) => {
    state.schoolId = schoolId
    localStorage.setItem('schoolId',schoolId);
  },
  SET_NAME: (state, admin_username) => {
    state.admin_username = admin_username
    localStorage.setItem('admin_username',admin_username);
  },
  SET_group_title: (state, group_title) => {
    state.group_title = group_title
    localStorage.setItem('group_title',group_title);
  },
  SET_ID: (state, id) => {
    state.id = id
    localStorage.setItem('id',id);
  },
  SET_AVATAR: (state, admin_fullname) => {
    state.admin_fullname = admin_fullname
    localStorage.setItem('admin_fullname',admin_fullname);
  },
  SET_accessRoutes: (state, routers) => {
    state.addRouters = routers
    state.routers = constantRoutes.concat(routers); //菜单显示
    localStorage.setItem('routers',JSON.stringify(routers))
  }
}

const actions = {
  login({commit}, userInfo) {
    let that = this
    const {username, password, loginIp} = userInfo
    return new Promise((resolve, reject) => {
      login({username: username.trim(), password: password, loginIp: loginIp}).then(response => {
        const {data} = response
        console.log(data);
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    }).catch((e) => {
    });
  },

  getInformations({commit, state}) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const {data} = response
        console.log(response);
        if ( !data ) {
          return reject('Verification failed, please Login again.')
        }
        const {admin_username, admin_fullname, authList, id, group_title} = data
        commit('SET_NAME', admin_username)
        commit('SET_AVATAR', admin_fullname)
        commit('SET_group_title', group_title)
        commit('SET_ID', id)
        commit('SET_accessRoutes', routerFormat(authList))
       /* localStorage.clear()//删除全部数据*/
        /*        commit('SET_accessRoutes',authList)*/
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    }).catch((e) => {
    });
  },
  logout({commit, state}) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    }).catch((e) => {
    });
  },

  // remove token
  resetToken({commit}) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    }).catch((e) => {
    });
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

